package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.wanpinghui.supplier.R;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.common.util.DensityUtil;


/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/8/23
 ***/

public class CountTextView extends AppCompatTextView {
    public CountTextView(Context context) {
        super(context);
        init();
    }

    public CountTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CountTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void init() {
        setBackground(ContextCompat.getDrawable(getContext(), R.drawable.shape_tv_bg_cart_count));
        setVisibility(GONE);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        if (TextUtil.isNotEmpty(text)) {
            setVisibility(VISIBLE);
            if (text.length() == 1) {
                this.setHeight(DensityUtil.dip2px(15));
                this.setWidth(DensityUtil.dip2px(15));
                super.setText(text, type);
            } else if (text.length() == 2) {
                this.setHeight(DensityUtil.dip2px(15));
                this.setWidth(DensityUtil.dip2px(22));
                super.setText(text, type);
            } else {
                this.setHeight(DensityUtil.dip2px(15));
                this.setWidth(DensityUtil.dip2px(30));
                super.setText("99+", type);
            }
        } else {
            setVisibility(GONE);
        }

    }

    public void setCount(Integer count) {
        if (count == null || count == 0) {
            setText("");
        } else {
            if (count > 99) {
                count = 99;
                setText(String.valueOf(count) + "+");
            } else {
                setText(String.valueOf(count));
            }

        }
    }
}
